package org.chen.wordwave.act;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;

import com.hjq.toast.Toaster;

import org.chen.wordwave.R;
import org.chen.wordwave.base.BaseActivity;
import org.chen.wordwave.init.WordInit;

import java.util.ArrayList;
import java.util.List;

public class Setting extends BaseActivity {
    private List<String> settings;

    private ListView lv_setting;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        settings=new ArrayList<>();
        settings.add(getString(R.string.Number_of_repeated_word_errors));
        settings.add(getString(R.string.test_word_count));
        settings.add(getString(R.string.my_info));
        settings.add(getString(R.string.update_pws));
        lv_setting.setAdapter(new MySettingAdapter());
        lv_setting.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        final EditText editText = new EditText(Setting.this);
                        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                        editText.setMaxLines(1);
                        AlertDialog.Builder inputDialog= new AlertDialog.Builder(Setting.this);
                        inputDialog.setTitle(R.string.how_many_words_start_to_repeat_after_a_word_error).setView(editText);
                        inputDialog.setPositiveButton(getString(R.string.yes),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        int number=10;
                                        try{
                                            number = Integer.parseInt(editText.getText().toString().trim());
                                        }catch (Exception e){
                                            Log.e("SettingActivity",e.toString());
                                            Toaster.show(R.string.InputErrorSystemSetToDefaultValueOf10);
                                        }finally {
                                            WordInit.flyerEditor.putInt("NumberOfRepeatedWords",number);
                                            WordInit.flyerEditor.commit();
                                        }


                                    }
                                }).show();
                        break;
                    case 1:
                        final EditText editText1 = new EditText(Setting.this);
                        editText1.setInputType(InputType.TYPE_CLASS_NUMBER);
                        editText1.setMaxLines(1);
                        AlertDialog.Builder inputDialog1= new AlertDialog.Builder(Setting.this);
                        inputDialog1.setTitle(R.string.PLEASE_ENTER_THE_NUMBER_OF_TEST_WORDS).setView(editText1);
                        inputDialog1.setPositiveButton(R.string.yes,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        int number=24;
                                        try{
                                            number = Integer.parseInt(editText1.getText().toString().trim());
                                            if (number>24){
                                                Toaster.show(R.string._24);
                                                number=24;
                                            }

                                            if (number<0){
                                                Toaster.show(R.string._0_24);
                                            }
                                        }catch (Exception e){
                                            Toaster.show(R.string.fail_24);
                                            number=24;
                                        }finally {
                                            WordInit.flyerEditor.putInt("TestNumberOfWords",number);
                                            WordInit.flyerEditor.commit();
                                        }


                                    }
                                }).show();
                        break;
                    case 2:
                        Intent intent = new Intent(Setting.this, UserProfile.class);
                        startActivity(intent);
                        break;
                    case 3:
                        startActivity(new Intent(Setting.this, UpdatePassWord.class));
                        break;
                }
            }
        });
    }


    @Override
    public Integer getLayoutResId() {
        return R.layout.setting_act;
    }

    @Override
    public void init() {

        lv_setting = findViewById(R.id.lv_setting);

    }


    class MySettingAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return settings.size();
        }

        @Override
        public Object getItem(int position) {
            return settings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            SettingViewHolder settingViewHolder=null;
            if (convertView==null){
                settingViewHolder= new SettingViewHolder();
                convertView= View.inflate(Setting.this, R.layout.item_setting,null);
                settingViewHolder.tv_text = convertView.findViewById(R.id.tv_text);
                convertView.setTag(settingViewHolder);
            }
            assert settingViewHolder != null;
            settingViewHolder.tv_text.setText(settings.get(position));
            return convertView;
        }


        static class SettingViewHolder{
            TextView tv_text;

        }
    }
}

